﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Yipusoft.Evaluate.DBUtility;
using Yipusoft.Evaluate.Model;

namespace Yipusoft.Evaluate.DAL
{
    public class Base
    {
        public bool Exsits(int BaseID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@BaseID",SqlDbType.Int)
            };
            parameters[0].Value = BaseID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Base_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public int Insert(BaseInfo Base)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@BaseID",SqlDbType.Int),
                new SqlParameter("@BaseName",SqlDbType.NVarChar,50),
                new SqlParameter("@TotalScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = Base.BaseName;
            parameters[2].Value = Base.TotalScore;
            parameters[3].Value = Base.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Base_Insert", parameters);
                return (int)parameters[0].Value;
            }
        }
        public bool Update(BaseInfo Base)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@BaseID",SqlDbType.Int),
                new SqlParameter("@BaseName",SqlDbType.NVarChar,50),
                new SqlParameter("@TotalScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Value = Base.BaseID;
            parameters[1].Value = Base.BaseName;
            parameters[2].Value = Base.TotalScore;
            parameters[3].Value = Base.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Base_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int BaseID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@BaseID",SqlDbType.Int)
            };
            parameters[0].Value = BaseID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Base_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public BaseInfo GetBase(int BaseID)
        {
            BaseInfo Base = null;
            SqlParameter[] parameters = {
                new SqlParameter("@BaseID",SqlDbType.Int)
            };
            parameters[0].Value = BaseID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Base_GetDetail", parameters);
                if (dr.Read())
                {
                    Base = new BaseInfo();
                    Base.BaseID = dr.GetInt32(0);
                    Base.BaseName = dr.GetString(1);
                    Base.TotalScore = dr.GetDecimal(2);
                    Base.Description = dr.GetString(3);
                }
                return Base;
            }
        }
        public List<BaseInfo> GetList(string StrWhere)
        {
            List<BaseInfo> Baselist = new List<BaseInfo>();
            BaseInfo Base = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Base_GetList", parameters);
                while (dr.Read())
                {
                    Base = new BaseInfo();

                    Base.BaseID = dr.GetInt32(0);
                    Base.BaseName = dr.GetString(1);
                    Base.TotalScore = dr.GetDecimal(2);
                    Base.Description = dr.GetString(3);
                    Baselist.Add(Base);

                }
                return Baselist;
            }
        }
    }
}
